package net.oschina.arvin.dbjava.type;

import net.oschina.arvin.dbjava.model.Column;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

/**
 * <pre>
 *     参考地址： https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html
 *
 *     以下情况会被认为是该类型：
 *     1. CHAR
 *     2. VARCHAR
 *     3. TINYTEXT
 *     4. TEXT
 *     5. MEDIUMTEXT
 *     6. LONGTEXT
 *     7. ENUM
 *     8. SET
 * </pre>
 *
 * @author Arvin
 * @time 2017/9/29 9:52
 */
public class StringDBJavaTypeConverter implements DBJavaTypeConverter {

    private static final Set<String> stringTypeSet = new HashSet<String>();

    static {
        stringTypeSet.add("CHAR");
        stringTypeSet.add("VARCHAR");
        stringTypeSet.add("TINYTEXT");
        stringTypeSet.add("TEXT");
        stringTypeSet.add("MEDIUMTEXT");
        stringTypeSet.add("LONGTEXT");
        stringTypeSet.add("ENUM");
        stringTypeSet.add("SET");
    }

    @Override
    public Class<?> convertToJavaType(Column column, boolean toBoolForLenOneNumber, boolean useUtilDate, boolean wrapMode) {
        String dataType = column.getDataType().toUpperCase();
        if (stringTypeSet.contains(dataType)) {
            return String.class;
        }
        return null;
    }

    @Override
    public String convertToDBColumnType(Class<?> javaType) {
        if (javaType == String.class || javaType == CharSequence.class || javaType.isEnum()) {
            return "VARCHAR(256)";
        }
        return null;
    }
}
